package com.ty.zls.adapter;

import android.content.Context;
import android.content.Intent;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.qmuiteam.qmui.layout.QMUILinearLayout;
import com.qmuiteam.qmui.util.QMUIDisplayHelper;
import com.qmuiteam.qmui.widget.roundwidget.QMUIRoundButton;
import com.ty.zls.R;
import com.ty.zls.activity.WaterActivity;
import com.ty.zls.bean.Account;
import com.ty.zls.bean.Constants;
import com.ty.zls.util.StringUtils;

import java.util.List;

public class NeedWaterAdapter extends RecyclerView.Adapter<NeedWaterAdapter.ViewHolder> {

    private int total;
    List<Account> datas;
    Context context;
    private OnItemClickListener mOnItemClickListener;

    private int pageNum = 1;
    private int pageSize = 5;

    public NeedWaterAdapter(List<Account> datas, Context context) {
        super();
        this.datas = datas;
        this.context = context;
    }

    @NonNull
    @Override
    public NeedWaterAdapter.ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        LayoutInflater inflater = LayoutInflater.from(viewGroup.getContext());
        QMUILinearLayout root = (QMUILinearLayout) inflater.inflate(R.layout.need_water_item, viewGroup, false);
        root.setRadiusAndShadow(QMUIDisplayHelper.dp2px(context, 15),
                QMUIDisplayHelper.dp2px(context, 14), 0.25f);
        return new ViewHolder(root);
    }

    @Override
    public void onBindViewHolder(@NonNull NeedWaterAdapter.ViewHolder viewHolder, int i) {
        Account account = datas.get(i);
        viewHolder.setId(account.getId());
        viewHolder.setName(account.getName());
        viewHolder.setTel(account.getTel());
        viewHolder.setSort(account.getSort());
        viewHolder.setPreSort(account.getPreSort());
        viewHolder.getEdit_pre_sort().setTag(i);
        viewHolder.getEdit_sort().setTag(i);
        viewHolder.getAdd_water().setTag(i);
        viewHolder.mTvWaterNo.setText(account.getMeterId() == null ? "" : account.getMeterId());
    }


    @Override
    public int getItemCount() {
        return datas.size();
    }

    public class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {

        private TextView water_id_tv;
        private TextView water_name_tv;
        private TextView water_tel_tv;
        private TextView water_sort_tv;
        private TextView mTvWaterNo;
        private QMUIRoundButton edit_pre_sort;
        private QMUIRoundButton edit_sort;
        private QMUIRoundButton add_water;

        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            water_id_tv = itemView.findViewById(R.id.water_id_tv);
            water_name_tv = itemView.findViewById(R.id.water_name_tv);
            water_tel_tv = itemView.findViewById(R.id.water_tel_tv);
            water_sort_tv = itemView.findViewById(R.id.water_sort_tv);
            edit_pre_sort = itemView.findViewById(R.id.edit_pre_sort);
            edit_sort = itemView.findViewById(R.id.edit_sort);
            add_water = itemView.findViewById(R.id.add_water);
            mTvWaterNo = itemView.findViewById(R.id.tv_water_no);

            /* 置后操作 */
            edit_pre_sort.setOnClickListener(this);
            /* 排序号修改 */
            edit_sort.setOnClickListener(this);
            /* 抄表 */
            add_water.setOnClickListener(this);
        }

        public QMUIRoundButton getAdd_water() {
            return add_water;
        }

        public QMUIRoundButton getEdit_pre_sort() {
            return edit_pre_sort;
        }

        public QMUIRoundButton getEdit_sort() {
            return edit_sort;
        }

        public void setId(String id) {
            water_id_tv.setText(id);
        }

        public void setName(String name) {
            water_name_tv.setText(name);
        }

        public void setTel(String tel) {
            water_tel_tv.setText(tel);
        }

        public void setSort(String sort) {
            water_sort_tv.setText(sort);
        }

        public void setPreSort(String preSort) {
            if (StringUtils.isEmpty(preSort) || "0".equals(preSort)) {
                edit_pre_sort.setText("置后");
            } else {
                edit_pre_sort.setText("取消置后");
            }
        }

        @Override
        public void onClick(View v) {
            int tag = (int) v.getTag();
            switch (v.getId()) {
                case R.id.edit_pre_sort:
                    mOnItemClickListener.onItemClick(Constants.PRE_SORT, tag, datas);
                    break;
                case R.id.edit_sort:
                    mOnItemClickListener.onItemClick(Constants.EDIT_SORT, tag, datas);
                    break;
                case R.id.add_water:
                    Account account = datas.get(tag);
                    Intent intent = new Intent(context, WaterActivity.class);
                    intent.putExtra("id", account.getId());
                    context.startActivity(intent);
                    break;
            }
        }
    }


    public List<Account> getDatas() {
        return datas;
    }

    public void setDatas(List<Account> datas) {
        this.datas = datas;
    }

    public int getTotal() {
        return total;
    }

    public void setTotal(int total) {
        this.total = total;
    }

    public int getPageNum() {
        return pageNum;
    }

    public void setPageNum(int pageNum) {
        this.pageNum = pageNum;
    }

    public int getPageSize() {
        return pageSize;
    }

    public void setPageSize(int pageSize) {
        this.pageSize = pageSize;
    }


    public void setOnItemClickListener(OnItemClickListener mOnItemClickListener) {
        this.mOnItemClickListener = mOnItemClickListener;
    }

    public interface OnItemClickListener {
        void onItemClick(String operation, int position, List<Account> accounts);
    }
}
